Learning Flex Basics (Part 4): Working with Data Models
Table of Contents
- Introduction
- Create the Application Object and Application Layout
- Add the Form Controls and the DataGrid Controls
- Add the New Employee Model, Employee, and Location Models
- Instantiate the DataController Class, and Create the Employee and DataController Classes
- Add the DataController Methods
- Code Listing for data.mxml
- Code Listings for DataController.as and Employee.as
- Code Listings for locations.xml and employee.xml
- Where to Go From Here
Add the DataController Methods
The DataController has three methods:
-
The
addNewEmp()method takes asrcobject that hasfName,lName, andlocationproperties and generates a temporary instance of the Employee class from it. The temporary object is added to adestdataProvider. Because thesrcanddestare passed as parameters, the method can handle copying data for both the button click and drag and drop operations.Note: Creating the temporary object rather than adding the src object directly is necessary because objects are passed by reference, and therefore any later changes to the src object would be automatically reflected in the dest dataProvider.
- The
doDragEnter()method handles the event of a dragged object passing over an object. On this event you define whether the object can accept the the dragged object using theevent.handledproperty (the default is "false") and if so, what DragManager operation should be carried out if the object is dropped (the default is NONE) - The
doDragDrop()method handles the event of a dragged object being dropped. The logic is facilitated by properties and methods of theeventobject that Flex automatically generates and passes to handlers:event.dragSource.dataForFormat("items")automatically returns an array containing the dragged data objectsevent.targetreturns the object capable of recieving the data in the location where the drop event occurs (in the case of a DataGrid, this property will be the grid's dataProvider)
-
After the DataController constructor, add a public method called addNewEmp() that takes two parameters called
srcanddest(both type Object) and returns nothing:public function addNewEmp(src:Object,dest:Object):Void { } -
Inside the method block, declare a variable called
tmpObjof type Employee, and initialize it as a new Employee object:var tmpObj:Employee=new Employee();
-
Invoke the Employee
addEmpData()method ontmpObj, passing thefName,lName, andlocationproperties ofsrcas arguments:tmpObj.addEmpData(src.fName,src.lName,src.location);
-
Invoke the dataProvider
addItem()method ondestto addtmpObj:dest.addItem(tmpObj);
-
Add another method called
doDragEnter()that takes a parameter called event; inside the method block setevent.handled=trueandevent.action=DragManager.COPY:public function doDragEnter(event):Void { event.handled=true; event.action=DragManager.COPY; } -
Add one more method called
doDragDrop()that also takes aneventparameter; inside the method block set a variable calleditemsequal toevent.dragSource.dataForFormat("items")and another variable calleddestequal toevent.target:public function doDragDrop(event):Void { var items = event.dragSource.dataForFormat("items"); var dest = event.target; } -
Add one more line to method that calls the
addNewEmp()method to copy the dragged data to the target DataGrid, passing the first element of theitemsarray anddestas arguments :addNewEmp(items[0],dest);
- You're done! Save the file, and then browse data.mxml application to test it. Try adding an new employee, and also dragging employees into the Engineering or Support Group DataGrids.